package com.general.framework.core.lang;

import com.general.framework.core.exception.Ex;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理工具类
 */
public abstract class Dates extends DateUtils {

    /**
     * 中文周数
     */
    public final static String[] WEEK_DAYS_CN = {"日", "一", "二", "三", "四", "五", "六"};

    /**
     * 格式化-时间
     */
    public final static String FMT_YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 格式化-时间
     */
    public final static String FMT_FULL_TIME = "yyyy-MM-dd HH:mm:ss";


    public final static String YYYY = "yyyy";

    public final static String YYYY_MM = "yyyy-MM";


    public final static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};


    /**
     * 时间格式化
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(final Date date, final String pattern) {
        return DateFormatUtils.format(date, pattern);
    }

    /**
     * 字符串转时间
     *
     * @param dateStr
     * @return
     */
    public static Date parseDate(final String dateStr, final String... parsePatterns) {
        try {
            return parseDate(dateStr, null, parsePatterns);
        } catch (ParseException e) {
            throw Ex.systemError(e, "parse date error");
        }
    }

    /**
     * 获取星期数
     *
     * @param date
     * @return
     */
    public static Integer getDayOfWeek(final Date date) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        final int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return day > 0 ? day : 0;
    }

    /**
     * 两个时间点相差的天数，不足1天返回1天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Integer getDifferentDays(final Date date1, final Date date2) {
        final Long diffTime = date1.getTime() - date2.getTime();
        final Long diffDay = diffTime / MILLIS_PER_DAY;
        final Long modDay = diffTime % MILLIS_PER_DAY;
        if (modDay > 0) {
            return diffDay.intValue() + 1;
        }
        return diffDay.intValue();
    }

    /**
     * 获取指定时间最早时间
     *
     * @param date
     * @return
     */
    public static Date getFirstTime(final Date date) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取指定时间最晚时间
     *
     * @param date
     * @return
     */
    public static Date getLastTime(final Date date) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取昨天开始时间
     *
     * @return
     */
    public static Date getYesterdayFirstTime() {
        final Date firstTime = getFirstTime(new Date());
        return Dates.addDays(firstTime, -1);
    }

    /**
     * 获取昨天开始时间
     *
     * @return
     */
    public static Date getYesterdayLastTime() {
        final Date lastTime = getLastTime(new Date());
        return Dates.addDays(lastTime, -1);
    }

    /**
     * 获取所在时间点周的开始时间
     *
     * @param date
     * @return
     */
    public static Date getWeekFirstTime(final Date date) {
        final Calendar calendar = Calendar.getInstance();
        final Date firstTime = getFirstTime(calendar.getTime());
        calendar.setTime(firstTime);
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        weekDay = weekDay == 0 ? 7 : weekDay;
        calendar.add(Calendar.DATE, 1 - weekDay);
        return calendar.getTime();
    }

    /**
     * 获取本周 最后一天
     *
     * @param date
     * @return
     */
    public static Date getWeekLastTime(final Date date) {
        final Date lastTime = getLastTime(date);
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(lastTime);
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        weekDay = weekDay == 0 ? 7 : weekDay;
        calendar.add(Calendar.DATE, 7 - weekDay);
        return calendar.getTime();
    }

    /**
     * 获取月份第一天
     *
     * @param date
     * @return
     */
    public static Date getMonthFirstTime(final Date date) {
        final Date firstTime = getFirstTime(date);
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(firstTime);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取月分第一天
     *
     * @param date
     * @return
     */
    public static Date getMonthLastTime(final Date date) {
        final Date lastTime = getLastTime(date);
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(lastTime);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getTimeNow() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDateNowStr() {
        return dateTimeNow(FMT_YYYY_MM_DD);
    }

    public static final String getTimeNowStr() {
        return dateTimeNow(FMT_FULL_TIME);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(FMT_YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }



    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(String str) {
        if (Strings.isEmpty(str)) {
            return null;
        }
        try {
            return parseDate(str, parsePatterns);
        } catch (Exception e) {
            throw Ex.systemError("日期转换异常");
        }
    }

    public static Date parseDate(Long timestamp) {
        return new Date(timestamp);
    }
    public static String parseDateToStr(Long timestamp) {
        return Dates.format(Dates.parseDate(timestamp), FMT_FULL_TIME);
    }

    public static String getTimeAgo(Date time) {
        long delta = (new Date().getTime() - time.getTime()) / 1000;
        //通常情况不存在，不排除数据库和服务器不在同一节点问题
        if (delta <= 0) {
            return "刚刚";
        }
        if (delta / (60 * 60 * 24 * 365) > 0) {
            return Dates.format(time, "yyyy-MM-dd");
        }
        if (delta / (60 * 60 * 24 * 30) > 0) {
            return Dates.format(time, "yyyy-MM-dd");
        }
        if (delta / (60 * 60 * 24 * 7) > 0) {
            return Dates.format(time, "yyyy-MM-dd");
        }
        if (delta / (60 * 60 * 24) > 0) {
            return delta / (60 * 60 * 24) + "天前";
        }
        if (delta / (60 * 60) > 0) {
            return delta / (60 * 60) + "小时前";
        }
        if (delta / (60) > 0) {
            return delta / (60) + "分钟前";
        }
        return "刚刚";
    }

}
